<template>
  <div id="index">
    <el-container>
      <el-header>
        <div class="login">
          <img src="../assets/logo.ec55f206.png" alt />
        </div>
        <div class="title">
          <h1>电商后台管理系统</h1>
        </div>
        <div class="logout">
          <span>欢迎观临~</span>
          <a @click="logout" href="javascript:;">退出</a>
        </div>
      </el-header>
      <el-container>
        <el-aside width="200px">
          <el-menu
            router
            :default-active= 'active'
            class="el-menu-vertical-demo"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#ffd04b"
          >
            <el-submenu v-for = "menu in menuLinst" :key = 'menu.id' :index="menu.path">
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>{{menu.authName}}</span>
              </template>

              <el-menu-item :index="item.path" v-for ='item in menu.children' :key = 'item.id' >
                <i class="el-icon-menu"></i>
                <span slot="title">{{ item.authName }}</span>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data () {
    return {
      menuLinst: []
    }
  },
  computed: {
    active () {
      // console.log(this.$route)
      // 高亮
      return this.$route.path.slice(1).split('-')[0]
    }
  },
  created () {
    this.$axios
      .get('menus')
      .then(res => {
        // console.log(res.data)
        const { data, meta } = res
        if (meta.status === 200) {
          this.menuLinst = data
        } else {
          this.$message.error(meta.msg)
        }
      }).catch((err) => {
        console.log(err)
      })
  },
  methods: {
    logout () {
      this.$confirm('亲是否退出登录?', '温馨提示', {
        type: 'warning'
      })
        .then(() => {
          this.$message.success('退出成功')
          localStorage.removeItem('token')
          this.$router.push('/login')
        })
        .catch(err => {
          console.log(err)
        })
    }
  }
}
</script>

<style lang = 'less' scoped>
#index {
  height: 100%;
  width: 100%;
  .el-container {
    height: 100%;
    width: 100%;
    .el-header {
      background-color: #d8d8d8;
      display: flex;
      .login {
        width: 180px;
        padding: 10px;
        img {
          height: 40px;
        }
      }
      .title {
        flex: 1;
        height: 60px;
        line-height: 60px;
        text-align: center;
        color: #545c64;
      }
      .logout {
        width: 180px;
        font-weight: 700;
        text-align: right;
        height: 60px;
        line-height: 60px;
        a {
          color: orange;
        }
      }
    }
    .el-container {
      background-color: #545c64;
      .el-menu{
        border: none;
      }
    }
    .el-main {
      background-color: #ecf0f1;
    }
  }
}
</style>
